package com.grgbanking.it.system.entity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.grgbanking.it.common.Constant;

/**
 * 权限码
 * 
 * Authority
 * 
 * @author 潘广伟
 * @Email p_3er@qq.com
 * @Date 2014-12-26 下午3:11:33
 * 
 * @version 1.0.0
 *
 */
public class Authority implements java.io.Serializable{
	private List<Role> roles;
	private List<Resource> resources;
	private Map<String, Integer> moduleAutorityMap;
	
	public Authority(User user){
		this.roles = user.getRoles();
		this.resources = user.getResources();
		this.moduleAutorityMap = new HashMap<String, Integer>();
	}
	
	/**
	 * 获取全局权限码
	 * getTopAuthority
	 *  
	 * @exception 
	 * @since  1.0.0
	 */
	public Integer getTopAuthority(){
		int code = Constant.AUTHORITY_PERSONAL;
		
		if(this.roles != null){
			for(Role role : roles){
				if(role.getState() < code){
					code = role.getState();
				}
			}
		}
		
		return code;
	}
	
	/**
	 * 是否为一般权限
	 * isCommon
	 * 
	 * @param moduleName
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public boolean isCommon(String moduleName){
		return isAuthority(moduleName, Constant.MODULE_AUTHORITY_COMMON);
	}
	/**
	 * 是否为个人权限
	 * isPersonal
	 * 
	 * @param moduleName
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public boolean isPersonal(String moduleName){
		return isAuthority(moduleName, Constant.MODULE_AUTHORITY_PERSONAL);
	}
	/**
	 * 是否为部门权限
	 * isDepartment
	 * 
	 * @param moduleName
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public boolean isDepartment(String moduleName){
		return isAuthority(moduleName, Constant.MODULE_AUTHORITY_DEPARTMENT);
	}
	
	/**
	 * 是否为公司权限
	 * isCompany
	 * 
	 * @param moduleName
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public boolean isCompany(String moduleName){
		return isAuthority(moduleName, Constant.MODULE_AUTHORITY_COMPANY);
	}
	
	/**
	 * 判断模块权限
	 * isAuthority
	 * 
	 * @param moduleName
	 * @param code
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	private boolean isAuthority(String moduleName,Integer code){
		boolean isAuthority = false;
		
		if(getModuleAuthority(moduleName) == code){
			isAuthority = true;
		}
		
		return isAuthority;
	}
	
	/**
	 * 获取模块权限码
	 * getModuleAuthority
	 * 
	 * @param moduleName
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	private Integer getModuleAuthority(String moduleName){
		Integer code = moduleAutorityMap.get(moduleName); 
		if(code == null){
			Integer parent = 0;
			code = Constant.MODULE_AUTHORITY_COMMON;
			if(resources != null){
				for(Resource resource : resources){
					if(resource.getName().equals(moduleName)){
						parent = resource.getId();
						break;
					}
				}
				
				if(parent != 0){
					for(Resource resource : resources){
						if(resource.getParent() == parent && resource.getState() == 3){
							if(resource.getUrl().equals("personal")){
								code = Constant.MODULE_AUTHORITY_PERSONAL;
								break;
							}
							if(resource.getUrl().equals("department")){
								code = Constant.MODULE_AUTHORITY_DEPARTMENT;
								break;
							}
							if(resource.getUrl().equals("company")){
								code = Constant.MODULE_AUTHORITY_COMPANY;
								break;
							}
							if(resource.getUrl().equals("common")){
								code = Constant.MODULE_AUTHORITY_COMMON;
								break;
							}
							
						}
					}
				}
			}
			
			moduleAutorityMap.put(moduleName, code);
		}
		
		return code;		
	}
}
